#!/usr/local/bin/python
import sys,os

def gbk2utf8(src, dst):
    if (os.path.isfile(src) == False):
        print '%s file must be a real file' % (src,)
        sys.exit(-1)
    if (os.path.exists(dst) == True):
        print '%s already exists' % (dst,)
        sys.exit(-1)
    src_f = open(src, "r")
    dst_f = open(dst, "w")
    err_f = open('%s.err' % (os.path.basename(dst),), "w")
    while (True):
        line = src_f.readline()
        if (not line):
            break
        try:
            line = unicode(line, encoding="gbk").encode("utf8")
        except:
            err_f.write(line)
            continue
        dst_f.write(line)
    
if __name__ == '__main__':
    if (len(sys.argv) < 3):
        print 'Usage arguments: src_file dst_file'
        sys.exit(-1)
    gbk2utf8(sys.argv[1], sys.argv[2])
